home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / src / !runtime / stacks.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-18  |  1.5 KB  |  61 lines  |  [TEXT/R*ch]

  1. /* To initialize and resize the stacks */
  2.  
  3. #include <string.h>
  4. #include "config.h"
  5. #include "fail.h"
  6. #include "misc.h"
  7. #include "mlvalues.h"
  8. #include "stacks.h"
  9. #include "debugger.h"
  10.  
  11. value * stack_low;
  12. value * stack_high;
  13. value * stack_threshold;
  14. value * extern_sp;
  15. value * trapsp;
  16. value global_data;
  17.  
  18. void init_stack()
  19. {
  20.   stack_low = (value *) stat_alloc(Stack_size);
  21.   stack_high = stack_low + Stack_size / sizeof (value);
  22.   stack_threshold = stack_low + Stack_threshold / sizeof (value);
  23.   extern_sp = stack_high;
  24.   trapsp = stack_high;
  25. }
  26.  
  27. void realloc_stack()
  28. {        
  29.   asize_t size;
  30.   value * new_low, * new_high, * new_sp;
  31.   value * p;
  32.  
  33.   Assert(extern_sp >= stack_low);
  34.   size = stack_high - stack_low;
  35.   if (size >= Max_stack_size)
  36.     raise_out_of_memory();
  37.   size *= 2;
  38.   gc_message ("Growing stack to %ld kB.\n",
  39.           (long) size * sizeof(value) / 1024);
  40.   new_low = (value *) stat_alloc(size * sizeof(value));
  41.   new_high = new_low + size;
  42.  
  43. #define shift(ptr) \
  44.     ((char *) new_high - ((char *) stack_high - (char *) (ptr)))
  45.  
  46.   new_sp = (value *) shift(extern_sp);
  47.   bcopy((char *) extern_sp,
  48.         (char *) new_sp,
  49.         (stack_high - extern_sp) * sizeof(value));
  50.   stat_free((char *) stack_low);
  51.   trapsp = (value *) shift(trapsp);
  52.   for (p = trapsp; p < new_high; p = Trap_link(p))
  53.     Trap_link(p) = (value *) shift(Trap_link(p));
  54.   stack_low = new_low;
  55.   stack_high = new_high;
  56.   stack_threshold = stack_low + Stack_threshold / sizeof (value);
  57.   extern_sp = new_sp;
  58.  
  59. #undef shift
  60. }
  61.